package com.travel.server.manager.impl.command.customer;

import com.travel.common.CommonConstant;
import com.travel.server.manager.dto.request.customer.UpdateTagInfoRequest;
import com.travel.server.manager.dto.response.customer.UpdateTagInfoResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.TagDal;
import com.travel.server.manager.model.TagInfo;
import com.travel.server.manager.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/9 18:09
 * @Desc:
 */
@Component
@Slf4j
public class UpdateTagInfoCommand extends AbstractCommand<UpdateTagInfoRequest, UpdateTagInfoResponse> {

    @Autowired
    private TagDal tagDal;

    @Override
    protected void checkInput(UpdateTagInfoRequest request) {

    }

    @Override
    public UpdateTagInfoResponse handle(UpdateTagInfoRequest request) {
        UserInfo userInfo = getUserInfo();
        Date now = new Date();

        Integer id = request.getId();

        TagInfo tagInfo;
        if(id == null){
            tagInfo = new TagInfo();
            tagInfo.setCreateBy(userInfo.getName());
            tagInfo.setCreateTime(now);
            tagInfo.setState(CommonConstant.YES);
        }else{
            tagInfo = tagDal.selectById(id);
        }
        tagInfo.setName(request.getTagName());
        tagInfo.setExplains(request.getTagExplains());
        tagInfo.setUpdateBy(userInfo.getName());
        tagInfo.setUpdateTime(now);

        id = tagDal.insertOrUpdate(tagInfo);

        UpdateTagInfoResponse response = new UpdateTagInfoResponse();
        response.setId(id);

        return response;
    }
}
